<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: light)">
<meta name="theme-color" content="#222" media="(prefers-color-scheme: dark)"><meta name="generator" content="Hexo 5.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.1.2/css/all.min.css" integrity="sha256-xejo6yLi6vGtAjcMIsY8BHdKsLg7QynVlFMzdQgUuy8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.css" integrity="sha256-Vzbj7sDDS/woiFS3uNKo8eIuni59rjyNGtXfstRzStA=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"gz1234.gitee.io","root":"/","images":"/images","scheme":"Gemini","darkmode":true,"version":"8.12.3","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":"flat"},"bookmark":{"enable":true,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="vue mixin 同名钩子函数将合并为一个数组，因此都将被调用。另外，混入对象的钩子将在组件自身钩子之前调用。 数据对象在内部会进行递归合并，并在发生冲突时以组件数据优先 值为对象的选项，例如 methods、components，将被合并为同一个对象。两个对象键名冲突时，取组件对象的键值对  路由钩子函数有三种 全局钩子： beforeEach、 afterEach 单个路由里面的钩子： be">
<meta property="og:type" content="article">
<meta property="og:title" content="vue面试题">
<meta property="og:url" content="https://gz1234.gitee.io/2020/10/29/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/vue%E9%9D%A2%E8%AF%95%E9%A2%98/index.html">
<meta property="og:site_name" content="郭泽">
<meta property="og:description" content="vue mixin 同名钩子函数将合并为一个数组，因此都将被调用。另外，混入对象的钩子将在组件自身钩子之前调用。 数据对象在内部会进行递归合并，并在发生冲突时以组件数据优先 值为对象的选项，例如 methods、components，将被合并为同一个对象。两个对象键名冲突时，取组件对象的键值对  路由钩子函数有三种 全局钩子： beforeEach、 afterEach 单个路由里面的钩子： be">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-10-29T03:09:28.000Z">
<meta property="article:modified_time" content="2023-05-16T10:39:16.215Z">
<meta property="article:author" content="郭泽">
<meta property="article:tag" content="vue面试题">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://gz1234.gitee.io/2020/10/29/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/vue%E9%9D%A2%E8%AF%95%E9%A2%98/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://gz1234.gitee.io/2020/10/29/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/vue%E9%9D%A2%E8%AF%95%E9%A2%98/","path":"2020/10/29/前端面试题/vue面试题/","title":"vue面试题"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>vue面试题 | 郭泽</title>
  





  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">郭泽</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#vue-mixin"><span class="nav-number">1.</span> <span class="nav-text">vue mixin</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%B7%AF%E7%94%B1%E9%92%A9%E5%AD%90%E5%87%BD%E6%95%B0%E6%9C%89%E4%B8%89%E7%A7%8D"><span class="nav-number">2.</span> <span class="nav-text">路由钩子函数有三种</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#react-vue-%E4%B8%AD%E7%9A%84-key-%E7%9A%84%E4%BD%9C%E7%94%A8-%E5%86%85%E9%83%A8%E5%8E%9F%E7%90%86%E6%98%AF%E4%BB%80%E4%B9%88"><span class="nav-number">3.</span> <span class="nav-text">react&#x2F;vue 中的 key 的作用&#x2F;内部原理是什么</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#NextTick-%E5%8E%9F%E7%90%86"><span class="nav-number">4.</span> <span class="nav-text">NextTick 原理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue3-%E4%B8%BA%E4%BB%80%E4%B9%88%E6%AF%94vue2-%E5%BF%AB"><span class="nav-number">5.</span> <span class="nav-text">vue3 为什么比vue2 快</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#proxy-%E5%93%8D%E5%BA%94%E5%BC%8F"><span class="nav-number">5.1.</span> <span class="nav-text">proxy 响应式</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#patchFlag-%E9%9D%99%E6%80%81%E6%A0%87%E8%AE%B0"><span class="nav-number">5.2.</span> <span class="nav-text">patchFlag 静态标记</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#hoistStatic-%E9%9D%99%E6%80%81%E6%8F%90%E5%8D%87"><span class="nav-number">5.3.</span> <span class="nav-text">hoistStatic 静态提升</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#cacheHandler-%E7%BC%93%E5%AD%98%E5%87%BD%E6%95%B0"><span class="nav-number">5.4.</span> <span class="nav-text">cacheHandler 缓存函数</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#SSR%E4%BC%98%E5%8C%96-%E9%A2%84%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%8C%96%E5%A4%84%E7%90%86%EF%BC%88%E5%A4%A7%E9%87%8F%E7%9A%84%E9%9D%99%E6%80%81%E8%8A%82%E7%82%B9%EF%BC%89"><span class="nav-number">5.5.</span> <span class="nav-text">SSR优化 预字符串化处理（大量的静态节点）</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#tree-shaking"><span class="nav-number">5.6.</span> <span class="nav-text">tree shaking</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Composition-API%E5%92%8C-React-Hooks-%E5%AF%B9%E6%AF%94"><span class="nav-number">6.</span> <span class="nav-text">Composition API和 React Hooks 对比</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue3%E6%9C%89%E5%93%AA%E4%BA%9B%E6%96%B0%E7%9A%84%E7%BB%84%E4%BB%B6"><span class="nav-number">7.</span> <span class="nav-text">vue3有哪些新的组件</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%A6%96%E5%B1%8F%E5%8A%A0%E8%BD%BD%E5%A6%82%E4%BD%95%E4%BC%98%E5%8C%96%E7%9A%84"><span class="nav-number">8.</span> <span class="nav-text">首屏加载如何优化的</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Vue%E7%9A%84diff%E7%AE%97%E6%B3%95"><span class="nav-number">9.</span> <span class="nav-text">Vue的diff算法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue-%E4%BA%8C%E6%AC%A1%E5%B0%81%E8%A3%85UI%E7%BB%84%E4%BB%B6%E5%BA%93"><span class="nav-number">10.</span> <span class="nav-text">vue 二次封装UI组件库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#vue%E9%9D%A2%E8%AF%95%E9%A2%98"><span class="nav-number">11.</span> <span class="nav-text">vue面试题</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">郭泽</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">21</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">51</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gz1234.gitee.io/2020/10/29/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/vue%E9%9D%A2%E8%AF%95%E9%A2%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="郭泽">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="郭泽">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="vue面试题 | 郭泽">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          vue面试题
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-10-29 11:09:28" itemprop="dateCreated datePublished" datetime="2020-10-29T11:09:28+08:00">2020-10-29</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-05-16 18:39:16" itemprop="dateModified" datetime="2023-05-16T18:39:16+08:00">2023-05-16</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/" itemprop="url" rel="index"><span itemprop="name">前端面试题</span></a>
        </span>
          ，
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/vue/" itemprop="url" rel="index"><span itemprop="name">vue</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="vue-mixin"><a href="#vue-mixin" class="headerlink" title="vue mixin"></a>vue mixin</h2><ul>
<li>同名钩子函数将合并为一个数组，因此都将被调用。另外，混入对象的钩子将在组件自身钩子之前调用。</li>
<li>数据对象在内部会进行递归合并，并在发生冲突时以组件数据优先</li>
<li>值为对象的选项，例如 methods、components，将被合并为同一个对象。两个对象键名冲突时，取组件对象的键值对</li>
</ul>
<h2 id="路由钩子函数有三种"><a href="#路由钩子函数有三种" class="headerlink" title="路由钩子函数有三种"></a>路由钩子函数有三种</h2><ul>
<li>全局钩子： beforeEach、 afterEach</li>
<li>单个路由里面的钩子： beforeEnter、 beforeLeave</li>
<li>组件钩子：beforeRouteEnter、 beforeRouteUpdate、 beforeRouteLeave</li>
</ul>
<h2 id="react-vue-中的-key-的作用-内部原理是什么"><a href="#react-vue-中的-key-的作用-内部原理是什么" class="headerlink" title="react/vue 中的 key 的作用/内部原理是什么"></a>react/vue 中的 key 的作用/内部原理是什么</h2><ol>
<li>虚拟 DOM 的 key 的作用?<ol>
<li>简单说: key 是虚拟 DOM 对象的标识, 在更新显示时 key 起着极其重要的作用</li>
<li>详细说: 当列表数组中的数据发生变化生成新的虚拟 DOM 后, React 进行新旧虚拟 DOM 的 diff 比较<ul>
<li>key 没有变<br> item 数据没变, 直接使用原来的真实 DOM<br> item 数据变了, 对原来的真实 DOM 进行数据更新</li>
<li>key 变了销毁原来的真实 DOM, 根据 item 数据创建新的真实 DOM 显示(即使 item 数据没有变)</li>
</ul>
</li>
</ol>
</li>
<li>key 为 index 的问题<ol>
<li>添加/删除/排序 =&gt; 产生没有必要的真实 DOM 更新 ==&gt; 界面效果没问题, 但效率低</li>
<li>如果 item 界面还有输入框 =&gt; 产生错误的真实 DOM 更新 ==&gt; 界面有问题<br>注意: 如果不存在添加/删除/排序操作, 用 index 没有问题</li>
</ol>
</li>
<li>解决:<br>使用 item 数据的标识数据作为 key, 比如 id 属性值</li>
</ol>
<h2 id="NextTick-原理"><a href="#NextTick-原理" class="headerlink" title="NextTick 原理"></a>NextTick 原理</h2><p><code>nextTick</code> 可以让我们在下次 DOM 更新循环结束之后执行延迟回调，用于获得更新后的 DOM。</p>
<p>在 Vue 2.4 之前都是使用的 microtasks，但是 microtasks 的优先级过高，在某些情况下可能会出现比事件冒泡更快的情况，但如果都使用 macrotasks 又可能会出现渲染的性能问题。所以在新版本中，会默认使用 microtasks，但在特殊情况下会使用 macrotasks，比如 v-on。</p>
<p>对于实现 macrotasks ，会先判断是否能使用 <code>setImmediate</code> ，不能的话降级为 <code>MessageChannel</code> ，以上都不行的话就使用 <code>setTimeout</code></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">if</span> (<span class="keyword">typeof</span> setImmediate !== <span class="string">&#x27;undefined&#x27;</span> &amp;&amp; isNative(setImmediate)) &#123;</span><br><span class="line">  macroTimerFunc = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    setImmediate(flushCallbacks)</span><br><span class="line">  &#125;</span><br><span class="line">&#125; <span class="keyword">else</span> <span class="keyword">if</span> (</span><br><span class="line">  <span class="keyword">typeof</span> MessageChannel !== <span class="string">&#x27;undefined&#x27;</span> &amp;&amp;</span><br><span class="line">  (isNative(MessageChannel) ||</span><br><span class="line">    <span class="comment">// PhantomJS</span></span><br><span class="line">    MessageChannel.toString() === <span class="string">&#x27;[object MessageChannelConstructor]&#x27;</span>)</span><br><span class="line">) &#123;</span><br><span class="line">  <span class="keyword">const</span> channel = <span class="keyword">new</span> MessageChannel()</span><br><span class="line">  <span class="keyword">const</span> port = channel.port2</span><br><span class="line">  channel.port1.onmessage = flushCallbacks</span><br><span class="line">  macroTimerFunc = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    port.postMessage(<span class="number">1</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125; <span class="keyword">else</span> &#123;</span><br><span class="line">  macroTimerFunc = <span class="function">() =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">setTimeout</span>(flushCallbacks, <span class="number">0</span>)</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="vue3-为什么比vue2-快"><a href="#vue3-为什么比vue2-快" class="headerlink" title="vue3 为什么比vue2 快"></a>vue3 为什么比vue2 快</h2><p><a target="_blank" rel="noopener" href="https://template-explorer.vuejs.org/#eyJzcmMiOiI8ZGl2PkhlbGxvIFdvcmxkPC9kaXY+XHJcbjxkaXY+SGVsbG8gV29ybGQ8L2Rpdj5cclxuPGRpdj5IZWxsbyBXb3JsZDwvZGl2PlxyXG48ZGl2PkhlbGxvIFdvcmxkPC9kaXY+XHJcbjxkaXY+e3ttZXNzfX08L2Rpdj4iLCJvcHRpb25zIjp7ImhvaXN0U3RhdGljIjp0cnVlfX0=">https://template-explorer.vuejs.org/#eyJzcmMiOiI8ZGl2PkhlbGxvIFdvcmxkPC9kaXY+XHJcbjxkaXY+SGVsbG8gV29ybGQ8L2Rpdj5cclxuPGRpdj5IZWxsbyBXb3JsZDwvZGl2PlxyXG48ZGl2PkhlbGxvIFdvcmxkPC9kaXY+XHJcbjxkaXY+e3ttZXNzfX08L2Rpdj4iLCJvcHRpb25zIjp7ImhvaXN0U3RhdGljIjp0cnVlfX0=</a></p>
<h3 id="proxy-响应式"><a href="#proxy-响应式" class="headerlink" title="proxy 响应式"></a>proxy 响应式</h3><h3 id="patchFlag-静态标记"><a href="#patchFlag-静态标记" class="headerlink" title="patchFlag 静态标记"></a>patchFlag 静态标记</h3><ul>
<li>编译模板时，动态节点做标记</li>
<li>标记，分为不同的类型，如TEXT PROPS</li>
<li>diff 算法时，可以区分静态节点，以及不同类型的动态节点</li>
</ul>
<h3 id="hoistStatic-静态提升"><a href="#hoistStatic-静态提升" class="headerlink" title="hoistStatic 静态提升"></a>hoistStatic 静态提升</h3><ul>
<li>将静态节点的定义，提升到父作用域，缓存起来</li>
<li>多个相邻的静态节点，会被合并起来 变异优化</li>
<li>典型的拿空间换时间的优化策略</li>
</ul>
<h3 id="cacheHandler-缓存函数"><a href="#cacheHandler-缓存函数" class="headerlink" title="cacheHandler 缓存函数"></a>cacheHandler 缓存函数</h3><ul>
<li>缓存事件</li>
</ul>
<h3 id="SSR优化-预字符串化处理（大量的静态节点）"><a href="#SSR优化-预字符串化处理（大量的静态节点）" class="headerlink" title="SSR优化 预字符串化处理（大量的静态节点）"></a>SSR优化 预字符串化处理（大量的静态节点）</h3><ul>
<li>静态节点直接输出 绕过vdom</li>
<li>动态节点还是动态渲染</li>
</ul>
<h3 id="tree-shaking"><a href="#tree-shaking" class="headerlink" title="tree shaking"></a>tree shaking</h3><h2 id="Composition-API和-React-Hooks-对比"><a href="#Composition-API和-React-Hooks-对比" class="headerlink" title="Composition API和 React Hooks 对比"></a>Composition API和 React Hooks 对比</h2><p>从 React Hook 从实现的角度来看，React Hook 是基于 useState 的调用顺序来确定下一个 re 渲染时间状态从哪个 useState 开始，所以有以下几个限制</p>
<ul>
<li>不在循环中、条件、调用嵌套函数 Hook</li>
<li>你必须确保它总是在你这边 React Top level 调用函数 Hook</li>
<li>使用效果、使用备忘录 依赖关系必须手动确定</li>
</ul>
<p>和 Composition API 是基于 Vue 的响应系统，和 React Hook 相比</p>
<ul>
<li>在设置函数中，一个组件实例只调用一次设置，而 React Hook 每次重新渲染时，都需要调用 Hook，给 React 带来的 GC 比 Vue 更大的压力，性能也相对 Vue 对我来说也比较慢</li>
<li>Compositon API 你不必担心调用的顺序，它也可以在循环中、条件、在嵌套函数中使用</li>
<li>响应式系统自动实现依赖关系收集，而且组件的性能优化是由 Vue 内部完成的，而 React Hook 的依赖关系需要手动传递，并且依赖关系的顺序必须得到保证，让路 useEffect、useMemo 等等，否则组件性能会因为依赖关系不正确而下降。</li>
</ul>
<p>虽然Compoliton API看起来像React Hook来使用，但它的设计思路也是React Hook的参考。</p>
<h2 id="vue3有哪些新的组件"><a href="#vue3有哪些新的组件" class="headerlink" title="vue3有哪些新的组件"></a>vue3有哪些新的组件</h2><ul>
<li><p>Fragment<br>组件可以没有根标签, 内部会将多个标签包含在一个Fragment虚拟元素中<br>减少标签层级, 减小内存占用</p>
</li>
<li><p>Teleport</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&lt;teleport to=<span class="string">&quot;移动位置&quot;</span>&gt;</span><br><span class="line"></span><br><span class="line">   &lt;div v-<span class="keyword">if</span>=<span class="string">&quot;isShow&quot;</span> <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;mask&quot;</span>&gt;</span><br><span class="line">       &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;dialog&quot;</span>&gt;</span><br><span class="line">           &lt;h3&gt;我是一个弹窗&lt;/h3&gt;</span><br><span class="line">           &lt;button @click=<span class="string">&quot;isShow = false&quot;</span>&gt;关闭弹窗&lt;/button&gt;</span><br><span class="line">       &lt;/div&gt;</span><br><span class="line">   &lt;/div&gt;</span><br><span class="line">&lt;/teleport&gt;</span><br><span class="line"></span><br></pre></td></tr></table></figure>
</li>
<li><p>Suspense</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123;defineAsyncComponent&#125; <span class="keyword">from</span> <span class="string">&#x27;vue&#x27;</span></span><br><span class="line"> <span class="keyword">const</span> Child = defineAsyncComponent(<span class="function">()=&gt;</span><span class="keyword">import</span>(<span class="string">&#x27;./components/Child.vue&#x27;</span>))</span><br><span class="line"></span><br><span class="line"> &lt;template&gt;</span><br><span class="line">    &lt;div <span class="class"><span class="keyword">class</span></span>=<span class="string">&quot;app&quot;</span>&gt;</span><br><span class="line">        &lt;h3&gt;我是App组件&lt;/h3&gt;</span><br><span class="line">        &lt;Suspense&gt;</span><br><span class="line">            &lt;template v-slot:<span class="keyword">default</span>&gt;</span><br><span class="line">                &lt;Child/&gt;</span><br><span class="line">            &lt;/template&gt;</span><br><span class="line">            &lt;template v-slot:fallback&gt;</span><br><span class="line">                &lt;h3&gt;加载中.....&lt;/h3&gt;</span><br><span class="line">            &lt;/template&gt;</span><br><span class="line">        &lt;/Suspense&gt;</span><br><span class="line">    &lt;/div&gt;</span><br><span class="line"> &lt;/template&gt;</span><br></pre></td></tr></table></figure>

</li>
</ul>
<h2 id="首屏加载如何优化的"><a href="#首屏加载如何优化的" class="headerlink" title="首屏加载如何优化的"></a>首屏加载如何优化的</h2><ul>
<li>减小入口文件积</li>
<li>静态资源本地缓存</li>
<li>UI框架按需加载</li>
<li>图片资源的压缩</li>
<li>组件重复打包</li>
<li>开启GZip压缩</li>
<li>使用SSR</li>
</ul>
<h2 id="Vue的diff算法"><a href="#Vue的diff算法" class="headerlink" title="Vue的diff算法"></a>Vue的diff算法</h2><p>diff 算法是一种通过同层的树节点进行比较的高效算法</p>
<p>diff整体策略为：深度优先，同层比较<br>比较只会在同层级进行, 不会跨层级比较<br>比较的过程中，循环从两边向中间收拢</p>
<ul>
<li>当数据发生改变时，订阅者watcher就会调用patch给真实的DOM打补丁</li>
<li>通过isSameVnode进行判断，相同则调用patchVnode方法</li>
<li>patchVnode做了以下操作：<ul>
<li>找到对应的真实dom，称为el</li>
<li>如果都有都有文本节点且不相等，将el文本节点设置为Vnode的文本节点</li>
<li>如果oldVnode有子节点而VNode没有，则删除el子节点</li>
<li>如果oldVnode没有子节点而VNode有，则将VNode的子节点真实化后添加到el</li>
<li>如果两者都有子节点，则执行updateChildren函数比较子节点</li>
</ul>
</li>
<li>updateChildren主要做了以下操作：<ul>
<li>设置新旧VNode的头尾指针</li>
<li>新旧头尾指针进行比较，循环向中间靠拢，根据情况调用patchVnode进行patch重复流程、调用createElem创建一个新节点，从哈希表寻找 key一致的VNode 节点再分情况操作</li>
</ul>
</li>
</ul>
<p>diff整体策略为：深度优先，同层比较<br>比较只会在同层级进行, 不会跨层级比较<br>比较的过程中，循环从两边向中间收拢</p>
<ul>
<li>当数据发生改变时，订阅者watcher就会调用patch给真实的DOM打补丁</li>
<li>通过isSameVnode进行判断，相同则调用patchVnode方法</li>
<li>patchVnode做了以下操作：<ul>
<li>找到对应的真实dom，称为el</li>
<li>如果都有都有文本节点且不相等，将el文本节点设置为Vnode的文本节点</li>
<li>如果oldVnode有子节点而VNode没有，则删除el子节点</li>
<li>如果oldVnode没有子节点而VNode有，则将VNode的子节点真实化后添加到el</li>
<li>如果两者都有子节点，则执行updateChildren函数比较子节点</li>
</ul>
</li>
<li>updateChildren主要做了以下操作：<ul>
<li>设置新旧VNode的头尾指针</li>
<li>新旧头尾指针进行比较，循环向中间靠拢，根据情况调用patchVnode进行patch重复流程、调用createElem创建一个新节点，从哈希表寻找 key一致的VNode 节点再分情况操作</li>
</ul>
</li>
</ul>
<h2 id="vue-二次封装UI组件库"><a href="#vue-二次封装UI组件库" class="headerlink" title="vue 二次封装UI组件库"></a>vue 二次封装UI组件库</h2><p>1 二次组件必须继承原有组件的所有特性。<br>2 二次组件名必须见名知意。<br>3 自定义暴露出来的接口越简单越好。<br>4 留有自定义插槽，让用户可以自己选择。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&lt;template&gt;</span><br><span class="line">  &lt;div class&#x3D;&quot;my-input&quot;&gt;</span><br><span class="line">    &lt;el-input v-bind&#x3D;&quot;$attrs&quot; ref&#x3D;&quot;inp&quot;&gt;</span><br><span class="line">      &lt;template #[slotName]&#x3D;&quot;slotProps&quot; v-for&#x3D;&quot;(slot, slotName) in $slots&quot; &gt;</span><br><span class="line">          &lt;slot :name&#x3D;&quot;slotName&quot; v-bind&#x3D;&quot;slotProps&quot;&#x2F;&gt;</span><br><span class="line">      &lt;&#x2F;template&gt;</span><br><span class="line">    &lt;&#x2F;el-input&gt;</span><br><span class="line">  &lt;&#x2F;div&gt;</span><br><span class="line">&lt;&#x2F;template&gt;</span><br><span class="line"></span><br><span class="line">&lt;script setup&gt;</span><br><span class="line"></span><br><span class="line">&lt;&#x2F;script&gt;</span><br><span class="line"></span><br><span class="line"> script setup 语法  组件实例属性和方法无法调用</span><br><span class="line"> 可以通过defineExpose显式公开ref属性</span><br><span class="line"></span><br><span class="line"> 可以使用选项式语法 绑定this</span><br><span class="line"> &lt;script&gt;</span><br><span class="line">export default &#123;</span><br><span class="line">  mounted() &#123;</span><br><span class="line">  const entries &#x3D; Object.entries(this.$refs.inp);</span><br><span class="line">  for(let [key,value] of entries)&#123;</span><br><span class="line">    this[key] &#x3D; value</span><br><span class="line">  &#125;</span><br><span class="line"> </span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&lt;&#x2F;script&gt;</span><br></pre></td></tr></table></figure>

<h2 id="vue面试题"><a href="#vue面试题" class="headerlink" title="vue面试题"></a>vue面试题</h2><p>[vue面试题]<a target="_blank" rel="noopener" href="https://github.com/57code/vue-interview">https://github.com/57code/vue-interview</a></p>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kity@2.0.4/dist/kity.min.js"></script><script type="text/javascript" src="https://cdn.jsdelivr.net/npm/kityminder-core@1.4.50/dist/kityminder.core.min.js"></script><script defer="true" type="text/javascript" src="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.js"></script><link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/hexo-simple-mindmap@0.2.0/dist/mindmap.min.css">
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/vue%E9%9D%A2%E8%AF%95%E9%A2%98/" rel="tag"># vue面试题</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2020/10/29/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/webpack%E9%9D%A2%E8%AF%95%E9%A2%98/" rel="prev" title="webpack面试题">
                  <i class="fa fa-chevron-left"></i> webpack面试题
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2020/10/29/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E9%A2%98/node%E9%9D%A2%E8%AF%95%E9%A2%98/" rel="next" title="node面试题">
                  node面试题 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">郭泽</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancybox/3.5.7/jquery.fancybox.min.js" integrity="sha256-yt2kYMy0w8AbtF89WXb2P1rfjcP/HTHLT7097U8Y5b8=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script>

  
<script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.0/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.8/pdfobject.min.js","integrity":"sha256-tu9j5pBilBQrWSDePOOajCUdz6hWsid/lBNzK4KgEPM="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>

  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/9.1.3/mermaid.min.js","integrity":"sha256-TIYL00Rhw/8WaoUhYTLX9SKIEFdXxg+yMWSLVUbhiLg="}}</script>
  <script src="/js/third-party/tags/mermaid.js"></script>

  <script src="/js/third-party/fancybox.js"></script>


  





</body>
</html>
